<?php
/**
 * @author: zhouzhongyuan <435690026@qq.com>
 * @copyright 万虎网络
 * @link http://www.wanhunet.com
 */

namespace backend\models;


use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;

class Meetings extends \src\models\Meetings
{
    public $active_time = "";

    public function init()
    {
        parent::init();
        $this->autoUserId();
    }

    public function rules()
    {
        return ArrayHelper::merge([
            ["active_time", 'required'],
            ["active_time", 'validateActiveTime'],
            ["method", "validateMethod"],
        ], parent::rules());
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return ArrayHelper::merge([
            "active_time" => "开会时间"
        ], parent::attributeLabels());
    }

    public function validateActiveTime($attribute)
    {
        $activeTime = $this->active_time;
        if (empty($activeTime)) {
            $this->addError($attribute, '请选择会议的起止时间');
            return;
        }

        $activeTime = explode('-', $activeTime);
        if (count($activeTime) !== 2) {
            $this->addError($attribute, "时间格式不正确，请您在控件中选择");
            return;
        }

        $this->started_at = strtotime($activeTime[0]);
        $this->end_at = strtotime($activeTime[1]);
        return;
    }

    public function validateMethod()
    {
        $this->method = serialize($this->method);
    }

    public function autoUserId()
    {
        if (\Yii::$app->getUser()->getIsGuest()) {
            throw new ForbiddenHttpException();
        }
        $this->user_id = \Yii::$app->getUser()->getId();
    }

}